package com.lj.luoye.lotteryticket.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.ArrayList;
import java.util.List;

public class LotteryHistoryDao {
    public final static String table_name = "lottery_history";
    public DBHelper dbHelper;
    public Context context;
    String[] fields = new String[]{"id","date", "redBall1", "redBall2", "redBall3", "redBall4", "redBall5", "redBall6", "blueBall"};

    public LotteryHistoryDao(Context context) {
        this.dbHelper = new DBHelper(context);
        this.context = context;
    }

    public Unionlotto getByDate(String dateP){
        Unionlotto unionlotto =null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryHistoryDao.table_name,fields,"date=?",new String[]{dateP},null,null,null);
        if (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String redBall1 = cursor.getString(cursor.getColumnIndex("redBall1"));
            String redBall2 = cursor.getString(cursor.getColumnIndex("redBall2"));
            String redBall3 = cursor.getString(cursor.getColumnIndex("redBall3"));
            String redBall4 = cursor.getString(cursor.getColumnIndex("redBall4"));
            String redBall5 = cursor.getString(cursor.getColumnIndex("redBall5"));
            String redBall6 = cursor.getString(cursor.getColumnIndex("redBall6"));
            String blueBall = cursor.getString(cursor.getColumnIndex("blueBall"));
            List<String> redBallList=new ArrayList<>();
            redBallList.add(redBall1);
            redBallList.add(redBall2);
            redBallList.add(redBall3);
            redBallList.add(redBall4);
            redBallList.add(redBall5);
            redBallList.add(redBall6);
            unionlotto = new Unionlotto(id,date,"",redBallList,blueBall);
        }
        db.close();
        return unionlotto;
    }

    public List<Unionlotto> getAll(){
        List<Unionlotto> unionlottoList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(LotteryHistoryDao.table_name,fields,null,null,null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String redBall1 = cursor.getString(cursor.getColumnIndex("redBall1"));
            String redBall2 = cursor.getString(cursor.getColumnIndex("redBall2"));
            String redBall3 = cursor.getString(cursor.getColumnIndex("redBall3"));
            String redBall4 = cursor.getString(cursor.getColumnIndex("redBall4"));
            String redBall5 = cursor.getString(cursor.getColumnIndex("redBall5"));
            String redBall6 = cursor.getString(cursor.getColumnIndex("redBall6"));
            String blueBall = cursor.getString(cursor.getColumnIndex("blueBall"));
            List<String> redBallList=new ArrayList<>();
            redBallList.add(redBall1);
            redBallList.add(redBall2);
            redBallList.add(redBall3);
            redBallList.add(redBall4);
            redBallList.add(redBall5);
            redBallList.add(redBall6);
            Unionlotto unionlotto = new Unionlotto(id,date,"",redBallList,blueBall);
            unionlottoList.add(unionlotto);
        }
        db.close();
        return unionlottoList;
    }

    public int insert(Unionlotto unionlotto){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("date",unionlotto.date);
        int i=1;
        for(String redBall:unionlotto.getRedBalls()){
            contentValues.put("redBall"+i,redBall);
            i++;
        }
        contentValues.put("blueBall",unionlotto.blueBall);
        long id = db.insert(LotteryHistoryDao.table_name, null, contentValues);
        db.close();
        return (int) id;
    }

    public int clean(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count=db.delete(LotteryHistoryDao.table_name,null,null);
        db.close();
        return count;
    }

}
